﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Office.Tools.Ribbon;
using Excel = Microsoft.Office.Interop.Excel;
using easyExcel.Util;

namespace easyExcel
{
    public partial class MainRibbon
    {
        Action<Excel.Range> action;


        private void easyTools_Load(object sender, RibbonUIEventArgs e)
        {
            
        }

        private void btnTrim_Click(object sender, RibbonControlEventArgs e)
        {

            Edit(Util.StringUtil.Trim);

        }

        private void button2_Click(object sender, RibbonControlEventArgs e)
        {
            Edit(Util.StringUtil.TrimEnd);
        }

        private void button1_Click(object sender, RibbonControlEventArgs e)
        {
            action = Util.StringUtil.TrimStart;
            Edit(action);
        }


        private void Edit(Action<Excel.Range> action)
        {
            //Ranges ranges;
            Excel.Range range = Globals.ThisAddIn.Application.Selection;
            //foreach (Range ran in ranges)
            //{
            foreach (Excel.Range r in range)
            {

                if (r.Value != null)
                {
                    action.Invoke(r);
                }
                
            }
            //}
        }

        private void Adjust(Action<Excel.Range> action)
        {
            //Ranges ranges;
            Excel.Range range = Globals.ThisAddIn.Application.Selection;
            //foreach (Range ran in ranges)
            //{
            foreach (Excel.Range r in range.Rows)
            {
                //r.Application.Height = 36;
                
                action.Invoke(r);

            }
            //}
        }

        private void EditComment(Action<Excel.Range, string> action, string str)
        {
            Excel.Range range = Globals.ThisAddIn.Application.Selection;
            foreach (Excel.Range r in range)
            {

                action.Invoke(r, str);

            }
            //}
        }

        private void FileSystemHelper(Action<Excel.Range, string> action, string str)
        {
            Excel.Range range = Globals.ThisAddIn.Application.Selection;
            foreach (Excel.Range r in range)
            {

                action.Invoke(r, str);

            }
        }

        private void button3_Click(object sender, RibbonControlEventArgs e)
        {
            Adjust(ExcelFormatUtil.UpRowHeight);
        }

        private void button4_Click(object sender, RibbonControlEventArgs e)
        {
            Adjust(ExcelFormatUtil.DownRowHeight);
        }

        private void button5_Click(object sender, RibbonControlEventArgs e)
        {
            frmCommentInput frm = new frmCommentInput();
            System.Windows.Forms.DialogResult dialogResult = frm.ShowDialog();
            //System.Windows.Forms.MessageBox.Show(frm.CommentString);
            if (dialogResult == System.Windows.Forms.DialogResult.OK)
            {
                EditComment(ExcelEditUtil.AddComment, frm.CommentString);
                frm = null;           
            }
            
            //System.Windows.Forms.DialogResult dialogResult =  frm.ShowDialog();
        }

        private void button6_Click(object sender, RibbonControlEventArgs e)
        {
            EditComment(ExcelEditUtil.DeleteComment, "");
        }

        private void button7_Click(object sender, RibbonControlEventArgs e)
        {
            frmFolderBrowser frm = new frmFolderBrowser();
            System.Windows.Forms.DialogResult dialogresult = frm.ShowDialog();

            if (dialogresult == System.Windows.Forms.DialogResult.OK)
            {
                
            }
        }

    }
}
